<?php
static $a;
// function add(){
// 	return $a++;
// }
// function get(){
// 	return $a;
// }
$server = new swoole_websocket_server("0.0.0.0", 9501);
$server->on('open', function($server, $req) {
	//add();
	echo "dump beging  ";
	var_dump($req);
	echo "dump end    ";
    echo "connection open: {$req->fd}\n";
    //$sum = get();
    //echo "sum:".$sum;
    //fd->header
    //fd->server
    echo "header begin   ";
    //var_dump($req->fd);

    echo "fd:".$req->fd;
    $req->fd = 2;
    echo "fd:".$req->fd;
    //$req->get("key");

    echo "header end  ";
});

$server->on('message', function($server, $frame) {
    echo "received message: {$frame->data}\n";
    //more
    foreach ($server->connections as $fd) {
        echo "guang loop begin";
    	echo "不确定是否写在message";
    	echo "也不确定有没有拿到 connections";
        $server->push(2, "你好"); 
        echo "guang loop end";
    }
    echo "frame里的fd:".$frame->fd;
    $server->push($frame->fd, json_encode(["hello", "world"]));
});

$server->on('close', function($server, $fd) {
    echo "connection close: {$fd}\n";
});

$server->start();